home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / widgets-mesa / SRC / MesaDrawingArea.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-15  |  8.5 KB  |  277 lines

  1. /* MesaDrawingArea.c -- Implementation file for the Mesa widget
  2.    Copyright (C) 1995, 1996 Thorsten.Ohl @ Physik.TH-Darmstadt.de
  3.  
  4.    This library is free software; you can redistribute it and/or
  5.    modify it under the terms of the GNU Library General Public
  6.    License as published by the Free Software Foundation; either
  7.    version 2 of the License, or (at your option) any later version.
  8.  
  9.    This library is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU Library General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU Library General Public
  15.    License along with this library; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.    $Id: MesaDrawingArea.c,v 1.20 1996/09/30 00:21:07 ohl Exp $
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <X11/IntrinsicP.h>
  24. #include <X11/StringDefs.h>
  25. #include <X11/Shell.h>
  26. #ifdef __GLX_MOTIF
  27. #include <GL/MesaMDrawingAreaP.h>
  28. #else
  29. #include <GL/MesaDrawingAreaP.h>
  30. #endif
  31. #ifndef GLwDebug
  32. #define GLwDebug(w) 0
  33. #endif
  34.  
  35. #ifdef __GLX_MOTIF
  36. #define GLwDrawingAreaWidget       GLwMDrawingAreaWidget
  37. #define GLwDrawingAreaClassRec     GLwMDrawingAreaClassRec
  38. #define GLwDrawingAreaRec          GLwMDrawingAreaRec
  39. #define glwDrawingAreaClassRec     glwMDrawingAreaClassRec
  40. #define glwDrawingAreaWidgetClass  glwMDrawingAreaWidgetClass
  41. #define MesaDrawingAreaWidget      MesaMDrawingAreaWidget
  42. #define MesaDrawingAreaClassRec    MesaMDrawingAreaClassRec
  43. #define MesaDrawingAreaRec         MesaMDrawingAreaRec
  44. #define mesaDrawingAreaClassRec    mesaMDrawingAreaClassRec
  45. #define mesaDrawingAreaWidgetClass mesaMDrawingAreaWidgetClass
  46. #endif
  47.  
  48.  
  49. /* Resources. */
  50.  
  51. #define offset(_field)  XtOffsetOf(MesaDrawingAreaRec, mesaDrawingArea._field)
  52. static XtResource resources[] =
  53. {
  54.   /* name, class, type, size,
  55.      offset, default_type, default_addr */
  56.   {GLwNximage, GLwCXImage, XtRBoolean, sizeof (Boolean),
  57.    offset (ximage), XtRBoolean, (caddr_t) False},
  58.   {GLwNshareLists, GLwCShareLists, XtRBoolean, sizeof (Boolean),
  59.    offset (share_lists), XtRBoolean, (caddr_t) False},
  60.   {GLwNshareListsWith, GLwCShareListsWith, XtRWidget, sizeof (Widget),
  61.    offset (share_lists_with), XtRWidget, NULL},
  62. };
  63. #undef offset
  64.  
  65. /* Basic widget methods.  */
  66.  
  67. /* Initialize this widget class.  Currently we just clear the pointer
  68.    to the widget we're sharing lists with.  */
  69.  
  70. static void
  71. ClassInitialize (void)
  72. {
  73.   MesaListsRoot = NULL;
  74. }
  75.  
  76. /* Initialize a widget instance.  */
  77.  
  78. static void
  79. Initialize (Widget request, Widget new, ArgList args, Cardinal * num_args)
  80. {
  81.   LOG (new);
  82.  ((MesaDrawingAreaWidget)new)->mesaDrawingArea.buffer = NULL;
  83. }
  84.  
  85. static void
  86. Realize (Widget w, XtValueMask *mask,
  87.      XSetWindowAttributes *attr)
  88. {
  89.   XMesaContext share_lists_with;
  90.   
  91.   LOG (w);
  92.  
  93.   /* Trying to outsmart ourselves with the cute looking
  94.      `(XtSuperclass (w)->core_class.realize) (w, mask, attr);'
  95.      is a BAD idea, because it will lead to infinite recursion if a
  96.      subclass has the same idea ...  */
  97.  
  98.   (glwDrawingAreaClassRec.core_class.realize) (w, mask, attr);
  99.  
  100.   if (MesaShareLists (w) && (MesaListsRoot != NULL))
  101.     {
  102.       if (GLwDebug(w))
  103.     fprintf (stderr, "sharing lists with %p...\n", MesaListsRoot);
  104.       share_lists_with = MesaContext (MesaListsRoot);
  105.     }
  106.   else
  107.     {
  108.       if (GLwDebug(w))
  109.     fprintf (stderr, "not sharing lists...\n");
  110.       share_lists_with = NULL;
  111.     }
  112.  
  113.   /* Attach a Mesa rendering context to the window */
  114.   MesaVisual (w) =
  115.     XMesaCreateVisual (XtDisplay (w),
  116.                ((MesaDrawingAreaWidget)w)->glwDrawingArea.visualInfo,
  117.                (GLboolean) MesaRGBA (w),
  118.                (MesaAlphaSize(w) > 0) ? GL_TRUE : GL_FALSE,
  119.                (GLboolean) MesaDoublebuffer (w),
  120.                        GL_FALSE, /* stereo */
  121.                (GLboolean) MesaXImage (w),
  122.                MesaDepthSize(w),
  123.                MesaStencilSize(w),
  124.                MesaAccumRedSize(w),
  125.                (GLint) 0 /* level */);
  126.   if (!MesaVisual (w))
  127.     {
  128.       printf ("Couldn't create Mesa/X visual!\n");
  129.       exit (1);
  130.     }
  131.  
  132.   MesaContext (w) = XMesaCreateContext (MesaVisual (w), share_lists_with);
  133.   if (!MesaContext (w))
  134.     {
  135.       printf ("Couldn't create Mesa/X context!\n");
  136.       exit (1);
  137.     }
  138.   if (MesaShareLists (w) && (MesaListsRoot == NULL))
  139.     {
  140.       if (GLwDebug(w))
  141.     fprintf (stderr, "installing %p as MesaListsRoot...\n", w);
  142.       MesaListsRoot = w;
  143.     }
  144.  
  145. #ifdef AVOID_MESABUFFER_INTERFACE
  146.   glXMakeCurrent (XtDisplay (w), XtWindow (w), MesaContext (w));
  147. #else
  148.   MesaBuffer (w) = XMesaCreateWindowBuffer (MesaVisual (w), XtWindow (w));
  149.   if (!MesaBuffer (w))
  150.     {
  151.       printf ("Couldn't create Mesa/X buffer!\n");
  152.       exit (1);
  153.     }
  154.  
  155.   XMesaMakeCurrent (MesaContext (w), MesaBuffer (w));
  156. #endif
  157.   if (GLwDebug(w))
  158.     fprintf (stderr, "finished with Realize() of %p.\n", w);
  159. }
  160.  
  161. static void
  162. Destroy (Widget w)
  163. {
  164.   LOG (w);
  165. #ifdef AVOID_MESABUFFER_INTERFACE
  166.   XMesaDestroyContext (MesaContext (w));
  167.   MesaContext (w) = NULL;
  168.   XMesaDestroyVisual (MesaVisual (w));
  169.   MesaVisual (w) = NULL;
  170. #else
  171.   XMesaDestroyBuffer (MesaBuffer (w));
  172.   MesaBuffer (w) = NULL;
  173.   XMesaDestroyContext (MesaContext (w));
  174.   MesaContext (w) = NULL;
  175.   XMesaDestroyVisual (MesaVisual (w));
  176.   MesaVisual (w) = NULL;
  177. #endif
  178. }
  179.  
  180. /* Resizing the widget can be done by adjusting the viewport.
  181.    However, we must make sure that the correct XMesaContext is
  182.    active.  It is also just common courtesy for an event handler
  183.    to restore the old XMesaContext afterwards.   */
  184.  
  185. static void
  186. Resize (Widget w)
  187. {
  188.   LOG (w);
  189.  
  190.   /* Ignore while there is no Mesa context.  */
  191.   if (XtIsRealized (w) && MesaContext (w))
  192.     {
  193. #ifdef AVOID_MESABUFFER_INTERFACE
  194.       GLXDrawable drawable = glXGetCurrentDrawable ();
  195.       GLXContext context = glXGetCurrentContext ();
  196.       glXMakeCurrent (XtDisplay (w), XtWindow (w), MesaContext (w));
  197.       glViewport (0, 0, w->core.width, w->core.height);
  198.       glXMakeCurrent (XtDisplay (w), drawable, context);
  199. #else
  200.       XMesaBuffer buffer = XMesaGetCurrentBuffer ();
  201.       XMesaContext context = XMesaGetCurrentContext ();
  202.       XMesaMakeCurrent (MesaContext (w), MesaBuffer (w));
  203.       glViewport (0, 0, w->core.width, w->core.height);
  204.       XMesaMakeCurrent (context, buffer);
  205. #endif
  206.     }
  207.  
  208.   /* Call our superclass' Resize method.  */
  209.   (glwDrawingAreaClassRec.core_class.resize) (w);
  210. }
  211.  
  212.  
  213. /* Now use all these methods in the widget class record.  */
  214.  
  215. MesaDrawingAreaClassRec mesaDrawingAreaClassRec =
  216. {
  217.   {
  218.     /* superclass            */ (WidgetClass) &glwDrawingAreaClassRec,
  219. #ifdef __GLX_MOTIF
  220.     /* class_name            */ "MesaMDrawingArea",
  221. #else
  222.     /* class_name            */ "MesaDrawingArea",
  223. #endif
  224.     /* widget_size           */ sizeof (MesaDrawingAreaRec),
  225.     /* class_initialize      */ ClassInitialize,
  226.     /* class_part_initialize */ NULL,
  227.     /* class_inited          */ FALSE,
  228.     /* initialize            */ Initialize,
  229.     /* initialize_hook       */ NULL,
  230.     /* realize               */ Realize,
  231.     /* actions               */ NULL,
  232.     /* num_actions           */ 0,
  233.     /* resources             */ resources,
  234.     /* num_resources         */ XtNumber (resources),
  235.     /* xrm_class             */ NULLQUARK,
  236.     /* compress_motion       */ TRUE,
  237.     /* compress_exposure     */ TRUE,
  238.     /* compress_enterleave   */ TRUE,
  239.     /* visible_interest      */ FALSE,
  240.     /* destroy               */ Destroy,
  241.     /* resize                */ (XtWidgetProc) Resize,
  242.     /* expose                */ XtInheritExpose,
  243.     /* set_values            */ NULL,
  244.     /* set_values_hook       */ NULL,
  245.     /* set_values_almost     */ XtInheritSetValuesAlmost,
  246.     /* get_values_hook       */ NULL,
  247.     /* accept_focus          */ NULL,
  248.     /* version               */ XtVersion,
  249.     /* callback_private      */ NULL,
  250.     /* tm_table              */ NULL,
  251.     /* query_geometry        */ XtInheritQueryGeometry,
  252.     /* display_accelerator   */ XtInheritDisplayAccelerator,
  253.     /* extension             */ NULL
  254.   },
  255. #ifdef __GLX_MOTIF
  256.   { /* XmPrimitive fields */
  257.     /* border_highlight      */ (XtWidgetProc) _XtInherit,
  258.     /* border_unhighlight    */ (XtWidgetProc) _XtInherit,
  259.     /* translations          */ XtInheritTranslations,
  260.     /* arm_and_activate      */ NULL,
  261.     /* get_resources         */ NULL,
  262.     /* num get_resources     */ 0,
  263.     /* extension             */ NULL,
  264.   },
  265. #endif /* __GLX_MOTIF */
  266.   { /* MesaDrawingArea fields*/
  267.     /* RCS_id                */
  268.     "@(#) $Id: MesaDrawingArea.c,v 1.20 1996/09/30 00:21:07 ohl Exp $"
  269.   }
  270. };
  271.  
  272. WidgetClass mesaDrawingAreaWidgetClass
  273.   = (WidgetClass) & mesaDrawingAreaClassRec;
  274.  
  275. /* The End. */
  276.  
  277.